﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MetaTracker.Model
{
    public class WeeklyStatistics
    {
        #region Data and Properties
        private DateTime _Date;
        public DateTime Date
        {
            get
            {
                return _Date;
            }
        }
        public string DisplayDate
        {
            get
            {
                return Date.ToShortDateString();
            }
        }


        private double? _WeightMax;
        private double? _WeightAvg;
        private double? _WeightMin;
        public double? WeightMax
        {
            get
            {
                return _WeightMax;
            }
        }
        public double? WeightAvg
        {
            get
            {
                return _WeightAvg;
            }
        }
        public double? WeightMin
        {
            get
            {
                return _WeightMin;
            }
        }

        private long? _CaloriesEaten;
        private long? _CaloriesBurned;
        private long? _CaloricDifference;
        public long? CaloriesEaten
        {
            get
            {
                return _CaloriesEaten;
            }
        }
        public long? CaloriesBurned
        {
            get
            {
                return _CaloriesBurned;
            }
        }

        public long? CaloricDifference
        {
            get
            {
                return _CaloricDifference;
            }
        }

        public double? ExpectedWeightChange
        {
            get
            {
                if (CaloricDifference == null)
                {
                    return null;
                }

                return CaloricDifference / 3500.0;
            }
        }


        private double? _ActualWeightChange;
        public double? ActualWeightChange
        {
            get
            {
                return _ActualWeightChange;
            }
            set
            {
                _ActualWeightChange = value;
            }
        }

        public string PercentOff
        {
            get
            {
                if (ExpectedWeightChange == null || ActualWeightChange == null)
                {
                    return "N/A";
                }
                else
                {
                    return String.Format("{0:00.0}%",
                        (Math.Abs(Math.Abs((double)ExpectedWeightChange - (double)ActualWeightChange) / 
                        (double)ActualWeightChange) * 100));
                }
            }

        }

        #endregion //Data and Properties

        #region Constructors

        public WeeklyStatistics()
        {
        }

        public WeeklyStatistics(DateTime date, IEnumerable<double> weights, IEnumerable<double> foodCalories,
            IEnumerable<double> exerciseCalories)
        {
            _Date = date;


            if (weights == null || weights.Count() == 0)
            {
                _WeightAvg = _WeightMax = _WeightMin = null;
            }
            else
            {
                _WeightMin = weights.Min();
                _WeightMax = weights.Max();
                _WeightAvg = weights.Average();
            }


            if (foodCalories == null || foodCalories.Count() == 0)
            {
                _CaloriesEaten = null;
            }
            else
            {
                _CaloriesEaten = (long)foodCalories.Sum();
            }

            if (exerciseCalories == null || exerciseCalories.Count() == 0)
            {
                _CaloriesBurned = null;
            }
            else
            {
                _CaloriesBurned = (long)exerciseCalories.Sum();
            }

            if (exerciseCalories == null || exerciseCalories.Count() == 0
                || foodCalories == null || foodCalories.Count() == 0)
            {
                _CaloricDifference = null;
            }
            else
            {
                _CaloricDifference = CaloriesEaten - CaloriesBurned;
            }

        }


        #endregion //Constructors
    }
}
